include constants.doh

/// Purpose: Generate the other T5 country migrant results(mostly regional level measures)


//////////////////////////////////////////////////
///////// Read in Other T5 Migrants Data /////////
//////////////////////////////////////////////////

// See the README for instructions on how to access this data
import delimited "input/pipeline/other_t5_regressions_input.csv", clear

replace curr_nuts3 = "_dummy" if curr_nuts3 == "`baseline_region'"
encode curr_nuts3, gen(nuts3_encoded)
fvset base last nuts3_encoded

// Make the total friend/group/cont produced controls
gen ln_other_frnd = log(total_30d_other_country_frnds + 1)
gen ln_other_grps = log(other_grps + 1)
gen ln_cont_prod = log(total_content_produced_yr + 1)

winsor2 n_frnd_nat_lcl, cuts(0 99) replace
winsor2 n_frnd_nat_50_lcl, cuts(0 99) replace
winsor2 pct_frnd_nat_lcl, cuts(0 99) replace
winsor2 n_lcl_50n_grps, cuts(0 99) replace
winsor2 shr_grps_lcl_50n, cuts(0 99) replace
winsor2 de_grps_avg_shr_nativ, cuts(0 99) replace
winsor2 pct_produ_de, cuts(0 99) replace
winsor2 pct_consu_de, cuts(0 99) replace
winsor2 n_frnd_sy_lcl, cuts(0 99) replace
winsor2 n_frnd_eu_lcl, cuts(0 99) replace
winsor2 n_frnd_t5_lcl, cuts(0 99) replace
winsor2 n_frnd_recent_t5_lcl, cuts(0 99) replace


////////////////////////////////////////
//// Individual level determinants /////
////////////////////////////////////////

eststo clear

eststo panel1: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 origin_fe) tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded origin_fe) tol(1e-5)
estadd ysumm
eststo panel3: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded origin_fe) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 origin_fe) tol(1e-5)
estadd ysumm
eststo panel5: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded origin_fe) tol(1e-5)
estadd ysumm
eststo panel6: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded origin_fe) tol(1e-5)
estadd ysumm

eststo panel7: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 origin_fe) tol(1e-5)
estadd ysumm
eststo panel8: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded origin_fe) tol(1e-5)
estadd ysumm
eststo panel9: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded origin_fe) tol(1e-5)
estadd ysumm

custom_xml_tab "output" "deters_baseline_recent_t5_migrant"

eststo clear


////////////////////////////////////
/// Get the NUTS3 coefficients ////
////////////////////////////////////

///////// First just the averages, Ns, and variances outside of a regression framework (main outcomes and other outcomes) //////////

preserve

    collapse (mean) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de n_frnd_sy_lcl n_frnd_eu_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl (count) n=n_frnd_nat_lcl, by(nuts3_encoded)

    export delimited "output/recent_t5_migrant_nuts3_avgs_and_Ns.csv"

restore

preserve

    collapse (sd) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de n_frnd_sy_lcl n_frnd_eu_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl, by(nuts3_encoded)

    export delimited "output/recent_t5_migrant_nuts3_sds.csv"

restore